// pages/register/index.js
import {
    getReLogin,
} from "../../api/index.js"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        options: [],
        show: false,
        checkState: false,
        active: "",
        typeList: [{
            name: "宠物店",
            id: 0
        }, {
            name: "医院",
            id: 1
        }, {
            name: "电商",
            id: 3
        }, {
            name: "其他",
            id: 2
        }],
        popupType: "",
        popupTitle: "",

        phone: '', //手机号
        registerType: '', // 注册类型
        pwd: '', // 密码
        confirmPwd: '', // 确认密码
        shopNames: '', // 店铺名称
        storeAddress: '', // 店铺地址
        address: '', // 店铺详细地址
        cascaderValue: "",
        register_identity: '',
        checked: false,
        time: 0,
        getCode: "",
        showMian: false,
        message: "",
        Code: '', //验证码
    },
    /**
     * 发送验证码回调函数方法
     */
    handleSendCode() {
        var that = this;
        if(this.data.phone == '') {
            this.showToastTip(), this.setData({
                message: "请填写完整信息后再提交发送"
            })
        } else {
            getReLogin({
                act: 'getcode',
                phone: this.data.phone,
            }).then(res => {
                this.showToastTip(),  this.setData({
                    message: "已发送"
                })
                console.log('发送', res)
                var times = 60
    
                if (res.result == 101) {
                    this.setData({
                        message: res.msg
                    })
                } else {
                    var i = setInterval(function () {
                        times--
                        if (times < 0) {
                            clearInterval(i)
                        } else {
                            that.setData({
                                time: times,
                                getCode: "重发" + times + "s",
                            })
                        }
                    }, 1000)
                }
            })
        }
    },
    /**
     * 选择回调函数方法
     * @param {*} e 
     */
    handleSelect(e) {
        const {
            type
        } = e.currentTarget.dataset
        switch (type) {
            case 'registerType':
                this.setData({
                    popupTitle: "类型",
                    popupType: type,
                    show: true
                })
                break;
            case 'address':
                this.setData({
                    popupTitle: "请选择省/市/区",
                    popupType: type,
                    show: true
                })
                break;
        }
    },

    handleCityChange: function (e) {
        console.log('用户点击了市级选项');
        console.log('选中的市级数据为：', e.detail);

        getReLogin({
            act: 'get_city_lists',
            id: e.detail.value
        }).then(res => {
            this.data.options.forEach(element => {
                element.children = []
                res.forEach(elementj => {
                    element.children.push({
                        text: elementj.name,
                        value: elementj.id
                    })
                });

            });
            console.log('options', this.data.options);
            this.setData({
                options: this.data.options
            })

        })
        // 在这里可以编写处理市级选项点击事件的逻辑
    },
    /**
     * 全部选项选择完成后触发
     * @param {*} e 
     */
    handleFinish(e) {
        console.log('触发', e)
        const {
            selectedOptions,
            value
        } = e.detail;
        this.data.cityid = e.detail.value
        const storeAddress = selectedOptions
            .map((option) => option.text || option.name)
            .join('/');
        this.setData({
            storeAddress,
            cascaderValue: value,
            show: false
        })
    },

    /**
     * 选择类型
     */
    handleCheck(e) {
        const {
            index,
            value
        } = e.currentTarget.dataset
        console.log('e', e)
        this.data.register_identity = value.id
        this.setData({
            active: index,
            registerType: value.name,
            checkState: !this.data.checkState,
            show: false
        })
        if (!this.data.checkState && this.data.show) {
            this.setData({
                active: "",
            })
        }
    },

    /**
     * 表单提交回调函数方法
     * @param {*} e 
     */
    handleSubmit(e) {
        const {
            value
        } = e.detail
        console.log('提交', value, this.data.phone);
        this.data.phone = value.phone

        if (!value.Code || !value.phone || !value.pwd || !value.confirmPwd || !value.shopNames || !this.data.cityid || !value.address) return this.showToastTip(), this.setData({
            message: "请填写完整信息后再提交注册"
        })
        if (!value.Code || !value.phone || !value.pwd || !value.confirmPwd || !value.shopNames || !this.data.cityid || !value.address) {
            return
        }
        let openid = wx.getStorageSync('OPENID');
        getReLogin({
            act: 'register',
            phone: this.data.phone,
            password: value.pwd,
            password2: value.confirmPwd,
            shopname: value.shopNames,
            code: value.Code, //验证码
            apptype: "H5",
            cityid: this.data.cityid,
            uid: openid,
            address: value.address,
            register_identity: this.data.register_identity,
        }).then(res => {
            if (res.shopid != '' && res.result != 101) {
                wx.navigateTo({
                    url: '/pages/login/index',
                })
            } else if(res.result == 101){
                
                this.showToastTip(), this.setData({
                    message:res.msg
                })
                
            } else {
                this.showToastTip(), this.setData({
                    message:res.msg
                })
                
            }
            console.log('注册', res)
        })
    },
    onInputLowPass(e) {
        console.log('e', e)
        this.data.Code = e.detail.value
    },
    onInputphone(e) {
        console.log('e', e.detail.value)
        this.data.phone = e.detail.value
    },
    /**
     * 提示
     */
    showToastTip() {
        var that = this;
        var times = 3
        var i = setInterval(function () {
            times--
            if (times < 0) {
                that.setData({
                    showMian: false
                })
                clearInterval(i)
            } else {
                that.setData({
                    showMian: true
                })
            }
        }, 1000)
    },
    // 省份列表
    getProvinceLists() {
        getReLogin({
            act: 'provinceLists',
        }).then(res => {
            this.data.options = res.map(item => {
                return {
                    text: item.name,
                    value: item.id,
                    children: []
                };
            });
            console.log('options', this.data.options);
            this.setData({
                options: this.data.options
            })

        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

        this.getProvinceLists();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})